package com.kye.express.business.jd_reservation.edit;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.databinding.ItemJdWaybillBinding;
import com.kye.express.view.reclyerview.OnItemClickListener;
import com.kye.model.bean.jd_reservation.JDWaybillsBean;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

public class JDWaybillViewBinder extends ItemViewBinder<JDWaybillsBean.JDWaybill, JDWaybillViewBinder.ViewHolder> {
    private OnItemClickListener<JDWaybillsBean.JDWaybill> mOnItemClickListener;

    @NonNull
    @Override

    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_jd_waybill, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull JDWaybillsBean.JDWaybill jDWaybill) {
        holder.bind(jDWaybill);
    }

    public OnItemClickListener<JDWaybillsBean.JDWaybill> getOnItemClickListener() {
        return mOnItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener<JDWaybillsBean.JDWaybill> onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private final ItemJdWaybillBinding mBind;

        ViewHolder(View itemView) {
            super(itemView);
            mBind = ItemJdWaybillBinding.bind(itemView);

            RxView.clicks(mBind.getRoot())
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (mOnItemClickListener == null) return;
                            mOnItemClickListener.onClickItem(mBind.getBean(), mBind.getRoot());
                        }
                    });
        }

        public void bind(JDWaybillsBean.JDWaybill jDWaybill) {
            mBind.setBean(jDWaybill);
        }
    }
}
